package com.horoscope.astrology.zodiac.signs.free.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Typeface
import android.util.AttributeSet
import com.horoscope.astrology.zodiac.signs.free.R


/**
 * 作者    1024
 * 时间    2020-01-19 15:21
 * 文件    constellation-android
 * 描述    设计中有改变字体样式,自定义随意改变字体样式ttf
 */
class TypeFaceTextView : androidx.appcompat.widget.AppCompatTextView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initTypefaceTextView(context, attrs!!)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private fun initTypefaceTextView(context: Context, attrs: AttributeSet) {
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView)
        val type = typedArray.getString(R.styleable.TypefaceTextView_typeface) ?: return
        when (type) {
            "JosefinSans-Bold" ->
                typeface = Typeface.createFromAsset(context.assets, "JosefinSans-Bold.ttf")

            "JosefinSans-Light" ->
                typeface = Typeface.createFromAsset(context.assets, "JosefinSans-Light.ttf")

            "JosefinSans-Regular" ->
                typeface = Typeface.createFromAsset(context.assets, "JosefinSans-Regular.ttf")

            "JosefinSans-SemiBold" ->
                typeface = Typeface.createFromAsset(context.assets, "JosefinSans-SemiBold.ttf")

            else -> setTypeface(Typeface.DEFAULT)
        }
        typedArray.recycle()
    }
}